{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>shoe size</th>\n",
       "      <th>height</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8.518110</td>\n",
       "      <td>73.029460</td>\n",
       "      <td>seniors</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.301527</td>\n",
       "      <td>68.959677</td>\n",
       "      <td>seniors</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7.386575</td>\n",
       "      <td>73.558042</td>\n",
       "      <td>seniors</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9.477281</td>\n",
       "      <td>68.195558</td>\n",
       "      <td>seniors</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10.910389</td>\n",
       "      <td>75.144672</td>\n",
       "      <td>seniors</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   shoe size     height    class\n",
       "0   8.518110  73.029460  seniors\n",
       "1  10.301527  68.959677  seniors\n",
       "2   7.386575  73.558042  seniors\n",
       "3   9.477281  68.195558  seniors\n",
       "4  10.910389  75.144672  seniors"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "train = pd.read_csv('train.csv')   # better be in the correct directory!\n",
    "test = pd.read_csv('test.csv')\n",
    "\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['seniors']\n",
      "['seniors']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/kernel/__main__.py:12: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "cols = ['shoe size', 'height']\n",
    "cols2 = ['class']\n",
    "trainArr = train.as_matrix(cols)\n",
    "trainRes = train.as_matrix(cols2)\n",
    "\n",
    "testArr = test.as_matrix(cols)\n",
    "testRes = test.as_matrix(cols2)\n",
    "\n",
    "knn = KNeighborsClassifier(n_neighbors=3, weights='distance')\n",
    "knn.fit(trainArr, trainRes)\n",
    "\n",
    "output = knn.predict(testArr)\n",
    "\n",
    "# or predict on a specific example!\n",
    "print(knn.predict(testArr[0]))\n",
    "print(testRes[0])                # a match!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9933333333333333"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "correct = 0.0\n",
    "\n",
    "for i in range(len(output)): \n",
    "    if testRes[i][0] == output[i]: \n",
    "        correct += 1\n",
    "    \n",
    "correct / len(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
